#!/usr/bin/env python
# Copyright (c) 2007-8 Qtrac Ltd. All rights reserved.
# This program or module is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as published
# by the Free Software Foundation, either version 2 of the License, or
# version 3 of the License, or (at your option) any later version. It is
# provided for educational purposes and is distributed in the hope that
# it will be useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
# the GNU General Public License for more details.

from __future__ import division
import platform
from PyQt4.QtCore import *
from PyQt4.QtGui import *

X11 = "qt_x11_wait_for_window_manager" in dir()


class ProgressSlider(QWidget):

    SWIDTH = 300
    SHEIGHT = 10

    def __init__(self, parent=None):
        super(ProgressSlider, self).__init__(parent)
        self.__position = 0
        self.__progress = 0
        self.setFocusPolicy(Qt.WheelFocus)
        self.setSizePolicy(QSizePolicy(QSizePolicy.MinimumExpanding,
                                       QSizePolicy.Fixed))


    def sizeHint(self):
        return self.minimumSizeHint()


    def minimumSizeHint(self):
        return QSize(ProgressSlider.SWIDTH, ProgressSlider.SHEIGHT)


    def setPosition(self, position):
        position = max(0, min(100, position))
        if position != self.__position:
            self.__position = position
            self.emit(SIGNAL("valueChanged(int,int)"),
                      self.__position, self.__progress)
            self.update()

    def setProgress(self, progress):
        progress = max(0, min(100, progress))
        if progress != self.__progress:
            self.__progress = progress
            self.emit(SIGNAL("valueChanged(int,int)"),
                      self.__position, self.__progress)
            self.update()


    def mousePressEvent(self, event):
        if event.button() == Qt.LeftButton:
            self.moveSlider(event.x())
            event.accept()
        else:
            QWidget.mousePressEvent(self, event)


#    def mouseMoveEvent(self, event):
#        self.moveSlider(event.x())


    def moveSlider(self, x):
        position = max(0, min(self.__progress, x / 3))
        if position != self.__position:
            self.__position = position
            self.emit(SIGNAL("valueChanged(int,int)"),
                      self.__position, self.__progress)
            self.update()

    def paintEvent(self, event=None):
        painter = QPainter(self)
        painter.setRenderHint(QPainter.Antialiasing)
        painter.setPen(self.palette().color(QPalette.Mid))
        painter.setBrush(self.palette().brush(QPalette.AlternateBase))
        painter.drawRect(self.rect())
        segColor = QColor(Qt.green).dark(120)
        segLineColor = segColor.dark()
        painter.setPen(segLineColor)
        painter.setBrush(segColor)
        painter.drawRect(0, 0, self.__progress*3, ProgressSlider.SHEIGHT)
        triangle = [QPointF(self.__position * 3-10, 0),
                    QPointF(self.__position * 3+10, 0),
                    QPointF(self.__position * 3, 10)]
        painter.setPen(Qt.yellow)
        painter.setBrush(Qt.darkYellow)
        painter.drawPolygon(QPolygonF(triangle))


if __name__ == "__main__":
    import sys

    app = QApplication(sys.argv)
    form = QDialog()
    sliderLabel = QLabel("&Fraction")
    slider = ProgressSlider()
    slider.setProgress(78)
    sliderLabel.setBuddy(slider)
    layout = QGridLayout()
    layout.addWidget(sliderLabel, 0, 0)
    layout.addWidget(slider, 0, 1, 1, 5)
    form.setLayout(layout)

    form.setWindowTitle("Fraction Slider")
    form.show()
    app.exec_()

